package com.springmybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.springmybatisplus.entity.Book;
import com.springmybatisplus.service.BookService;
import io.swagger.models.auth.In;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class BookController {
	@Resource
	BookService bookService;
	@GetMapping("/books")
	public String findAllBook(Model model){
		model.addAttribute("books",bookService.findAllBooks());
		return "/bookList";
	}
	@GetMapping("/book/{id}")
	public String findBookById(@PathVariable Integer id,Model model){
		Book book = bookService.findBookById(id);
		model.addAttribute("book",book);
		return "/book";
	}
	@GetMapping("/searchBooks")
	public String searchBook(Book book,Model model){
		model.addAttribute("books",bookService.searchBooks(book));
		return "/bookList";
	}
	@GetMapping("/bookPage")
	public String bookPage(@RequestParam(defaultValue = "1") Integer start,@RequestParam(defaultValue = "3") Integer size,Model model){
		model.addAttribute("page",bookService.getPage(start,size));
		return "/bookPage";
	}
	@GetMapping("/book2")
	public String findAllBook2(Model model){
		model.addAttribute("books",bookService.list());
		return "/bookList";
	}
	@GetMapping("/book2/{id}")
	public String findBookById2(@PathVariable Integer id,Model model){
		model.addAttribute("book",bookService.getById(id));
		return "/book";
	}
}
